﻿using System;
using System.Net;
using SomeKit.Http;
using SomeKit.Serialization;

namespace SomeKit.TypeSafe.BasicAuthentication
{
    public static partial class ClientHelper
    {
        /// <summary>
        /// Executes the "GET" verb against the resource located at <paramref name="uri"/>.
        /// </summary>
        /// <typeparam name="RESULT">The type of the result parameter.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="userName">The user name part of the credentials.</param>
        /// <param name="password">The password part of the credentials.</param>
        /// <param name="handler">The handler receiving the resultant object.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="deserializer">A method capable of deserializing the resultant object.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void Get<RESULT>(this Uri uri, string userName, string password, 
            Action<RESULT> handler, Action<Exception> errorHandler, Func<string, RESULT> deserializer, Action<HttpWebRequest> headerHandler = null)
        {
            uri.Get<RESULT>(handler, errorHandler, deserializer, headerHandler.AppendBasicAuthentication(userName, password));
        }
        /// <summary>
        /// Executes the "GET" verb against the resource located at <paramref name="uri"/>. The response is expected to be formatted in XML.
        /// </summary>
        /// <typeparam name="RESULT">The type of the result parameter.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="userName">The user name part of the credentials.</param>
        /// <param name="password">The password part of the credentials.</param>
        /// <param name="handler">The handler receiving the resultant object.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void GetXml<RESULT>(this Uri uri, string userName, string password,
            Action<RESULT> handler, Action<Exception> errorHandler, Action<HttpWebRequest> headerHandler = null)
        {
            uri.Get<RESULT>(handler, errorHandler, s => s.DeserializeXml<RESULT>(), headerHandler.AppendXmlAndBasicAuthentication(userName, password));
        }
        /// <summary>
        /// Executes the "GET" verb against the resource located at <paramref name="uri"/>. The response is expected to be formatted in JSON.
        /// </summary>
        /// <typeparam name="RESULT">The type of the result parameter.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="userName">The user name part of the credentials.</param>
        /// <param name="password">The password part of the credentials.</param>
        /// <param name="handler">The handler receiving the resultant object.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static void GetJson<RESULT>(this Uri uri, string userName, string password,
            Action<RESULT> handler, Action<Exception> errorHandler, Action<HttpWebRequest> headerHandler = null)
        {
            uri.Get<RESULT>(handler, errorHandler, s => s.DeserializeJson<RESULT>(), headerHandler.AppendJsonAndBasicAuthentication(userName, password));
        }
    }
}
